#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/io.h>
#include <unistd.h>
#include <sys/stat.h>
#include <pthread.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define SERPORT 9003 //服务器开放给我们的IP地址和端口号 
#define SERADDR "192.168.149.128" //本机IP地址
#define CON_NUM 5 //服务端最大连接数量
#define DEFAULT_BUFLEN 1024

char Client_id[6]; //定义字符串来表示客户ID
char Client_01[6] = "PCP01";
char Client_02[6] = "PCP02";
char Client_03[6] = "PCP03";
char Client_04[6] = "PCP04";
char Client_05[6] = "PCP05";

void* Recv_from_client(void* arg)
{
    int read_th;
    int write_th;
    int open_th;
    int Client_sock = *((int*)arg); //定义文件描述符
    int recvdata; //代表recv函数的返回值
    int senddata; //代表send函数的返回值
    char inp_01[13] = "request_data";
    char inp_02[5] = "over";
    char recv_buff[DEFAULT_BUFLEN] = {0}; //定义字符串来表示从客户受到的信息
    int recvbuff_len = DEFAULT_BUFLEN; //表示从客户端受到信息的长度
    //建立连接后开始通信
    printf("服务端开启新的线程用来通信！\n");
    printf("Start to receive!\n"); //提示开始接受信息
    recvdata = recv(Client_sock, Client_id, sizeof(Client_id), 0); //表示受到的客户ID
    printf("当前客户的ID为: %s\n", Client_id); //打印出该客户端ID
    //根据客户端发送的ID选择对应的文件来存放客户端发送的信息
    if(strcmp(Client_id, Client_01) ==0 )
    {
        open_th = open("txt_01.txt", O_RDWR);
        printf("客户端：%s的信息将会保存在文件：txt_01.txt中！\n", Client_id);
    }
    else if(strcmp(Client_id, Client_02) ==0 )
    {
        open_th = open("txt_02.txt", O_RDWR);
        printf("客户端：%s的信息将会保存在文件：txt_01.txt中！\n", Client_id);
    }
    else if(strcmp(Client_id, Client_03) ==0 )
    {
        open_th = open("txt_03.txt", O_RDWR);
        printf("客户端：%s的信息将会保存在文件：txt_01.txt中！\n", Client_id);
    }
    else if(strcmp(Client_id, Client_04) ==0 )
    {
        open_th = open("txt_04.txt", O_RDWR);
        printf("客户端：%s的信息将会保存在文件：txt_01.txt中！\n", Client_id);
    }
    else if(strcmp(Client_id, Client_05) ==0 )
    {
        open_th = open("txt_05.txt", O_RDWR);
        printf("客户端：%s的信息将会保存在文件：txt_01.txt中！\n", Client_id);
    }
    while(1)
    {
        recvdata = recv(Client_sock, recv_buff, recvbuff_len, 0); //接收客户端发送的时间信息
        if(recvdata != -1)
        {
            printf("%s, i've got it, thanks!\n", Client_id);
            write_th = write(open_th, recv_buff, sizeof(recv_buff)); //将发送的时间信息写入到指定文件内进行保存
            printf("服务器已将客户端：%s发送的信息写入到文件中\n", Client_id);
        }
        printf("准备接受客户端：%s下一步指示!\n", Client_id);
        recvdata = recv(Client_sock, &recv_buff, sizeof(recv_buff), 0); //接收客户端发送的指示信息
        printf("requ_buff=%s\n", recv_buff);
        //如果发送的信息为request_data，则将存放的信息返还给客户端
        if(strcmp(recv_buff, inp_01) == 0)
        {
            read_th = read(open_th, recv_buff, 1024);
            senddata = send(Client_sock, recv_buff, recvbuff_len, 0);
            printf("服务端已将保存的信息发送给客户端：%s!\n", Client_id);
        }
        //若发送的信息为over，则结束该线程
        else if(strcmp(recv_buff, inp_02) == 0)
        {
            printf("客户端：%s已关闭!\n", Client_id);
            pthread_exit(NULL);
        }
    }
}


int main()
{
    int write_th;
    int fd=open("Server_recv.txt", O_RDWR);
    int sockfd = -1, ret = -1,  clifd = -1, Recvdata = -1;
    socklen_t len = 0;
    struct sockaddr_in seraddr = {0};
    struct sockaddr_in cliaddr = {0};

    //用socket建立文件描述符
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd == -1)
    {
        perror("Socket");
        return -1;
    }
    else
    {
        printf("Socket success!\n");
    }

    seraddr.sin_family = AF_INET; //设置地址族为IPv4;
    seraddr.sin_port = htons(SERPORT); //设置地址的端口号信息，将主机字节顺序转换为网络字节顺序。
    seraddr.sin_addr.s_addr = inet_addr(SERADDR); //设置IP地址，用inet_addr将点分十进制的IP转换成一个长整数型数(u_long类型)

    //bind绑定sockfd和当前电脑的ip地址以及端口号
    ret = bind(sockfd, (const struct sockaddr *)&seraddr, sizeof(seraddr));
    if(ret < 0)
    {
        perror("bind");
        return -1;
    }
    else
    {
        printf("Bind success!\n");
    }

    //使用listen监听端口
    ret = listen(sockfd, CON_NUM);
    if(ret < 0)
    {
        printf("Listen error! \n");
        return -1;
    }
    else
    {
        printf("Listen success! \n");
    }

    while(1)
    {
        printf("Start to accept!!!\n");
        //使用accept阻塞等待客户端接入
        int Client_socket = accept(sockfd, (struct sockaddr *)&cliaddr, &len);
        printf("有客户端连接到服务器了：%s!\n", inet_ntoa(cliaddr.sin_addr)); //提示有客户端连接到服务器，并显示出客户端的IP
        // 当有新客户端连接的时候，开启新线程
        pthread_t THR;
		void *arg = (void*)&Client_socket; //定义出函数的参数
		ret = pthread_create(&THR, NULL, Recv_from_client, arg); //使用函数pthread_create创建多线程
    }
}
//潘